﻿using System;
using System.Collections.Generic;

namespace TidyBlog.MVC.WWW.Models
{
    /// <summary>
    /// 分页条
    /// </summary>
    public class Paginator
    {
        /// <summary>
        /// 分页条中显示的数字项目数
        /// </summary>
        protected const byte NUMBER_OF_PAGES_TO_SHOW = 7;
        /// <summary>
        /// 获取当前页
        /// </summary>
        public int CurrentPage { get; protected set; }
        /// <summary>
        /// 获取总页数
        /// </summary>
        public ushort TotalPages { get; protected set; }
        /// <summary>
        /// 获取分页条项目
        /// </summary>
        public PaginatorItem[] Items { get; protected set; }
        /// <summary>
        /// 链接模板
        /// </summary>
        protected string HrefTemplate;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="currentPage">当前页</param>
        /// <param name="totalPages">总页数</param>
        /// <param name="hrefTemplate">链接模版</param>
        public Paginator(int currentPage, ushort totalPages, string hrefTemplate)
        {
            this.CurrentPage = currentPage;
            this.TotalPages = totalPages;
            this.HrefTemplate = hrefTemplate;

            var numberOfPagesPerSide = NUMBER_OF_PAGES_TO_SHOW / 2;
            var start = currentPage - numberOfPagesPerSide;
            var end = currentPage + numberOfPagesPerSide;
            var startOverflow = start - 1;
            var endOverflow = totalPages - end;

            var items = new List<PaginatorItem>();

            if (startOverflow < 0)
            {
                start = 1;
                end = Math.Min(totalPages, end - startOverflow);
            }
            if (endOverflow < 0)
            {
                end = totalPages;
                if (startOverflow > 0) { start = Math.Max(1, start + endOverflow); }
            }

            if (end - start + 1 < NUMBER_OF_PAGES_TO_SHOW)
            {
                if (end < totalPages)
                {
                    end++;
                }
                else if (start > 1)
                {
                    start--;
                }
            }

            for (var i = start; i <= end; i++)
            {
                items.Add(
                    new PaginatorItem() { Number = i, IsCurrent = i == currentPage }
                );
		    }

            var temp = start - 1;
		    if (temp > 0)
            {
			    if (temp > 2)
                {
                    items.Insert(0, new PaginatorItem() { Number = -1, IsCurrent = false });
			    }
                else if (temp > 1)
                {
                    items.Insert(0, new PaginatorItem() { Number = 2, IsCurrent = false });
			    }
                items.Insert(0, new PaginatorItem() { Number = 1, IsCurrent = false });
		    }

            temp = totalPages - end;
		    if (temp > 0)
            {
			    if (temp > 2)
                {
				    items.Add(new PaginatorItem() { Number = -1, IsCurrent = false });
			    }
                else if (temp > 1)
                {
                    items.Add(new PaginatorItem() { Number = end + 1, IsCurrent = false });
			    }
                items.Add(new PaginatorItem() { Number = totalPages, IsCurrent = false });
		    }

            this.Items = items.ToArray();
        }

        /// <summary>
        /// 生成分页链接
        /// </summary>
        /// <param name="page">页码</param>
        /// <returns>分页链接</returns>
        public string GenerateHref(int page)
        {
            return String.Format(this.HrefTemplate, page.ToString());
        }
    }

    /// <summary>
    /// 分页条项目
    /// </summary>
    public class PaginatorItem
    {
        /// <summary>
        /// 获取或设置分页条项目文本
        /// </summary>
        public int Number { get; set; }
        /// <summary>
        /// 获取或设置是否当前页
        /// </summary>
        public bool IsCurrent { get; set; }
    }
}